//
//  TBCVideoViewController.m
//  待定
//
//  Created by BoyaLee on 15/11/5.
//  Copyright © 2015年 BoyaLee. All rights reserved.
//

#import "TBCVideoViewController.h"
#import "TBCVideoTableViewCell.h"
#import "TBCVideo.h"
#import <MJRefresh.h>
#import <MediaPlayer/MediaPlayer.h>
#import <MBProgressHUD.h>
#import "TBCAppTools.h"
#import "BaseTableView.h"
@interface TBCVideoViewController ()<UITableViewDataSource,UITableViewDelegate>


@property(nonatomic,strong)BaseTableView *tableView;
@property(nonatomic,strong)NSMutableArray *groupArray;

//上下拉
@property(nonatomic,assign)NSInteger count;
@property(nonatomic,assign)BOOL isUpLoading;

//菊花
@property(nonatomic,strong)MBProgressHUD *progress;

@end

@implementation TBCVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"视频";
    
    
    //数组初始化
    self.groupArray = [NSMutableArray array];
    
    self.tableView = [[BaseTableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.view addSubview:self.tableView];
    
    //菊花
    self.progress = [[MBProgressHUD alloc] initWithView:self.tableView];
    [self.progress setYOffset:-50];
    self.progress.labelText = @"正在加载";
    [self.tableView addSubview:self.progress];
    [self.progress show:YES];
    
    [self setupRefresh];

}


- (void)setupRefresh
{
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getHeaderData)];
    
    self.tableView.header = header;
    
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(getFooterData)];
    self.tableView.footer = footer;
    
    [self.tableView.header beginRefreshing];

}

-(void)getHeaderData
{
    self.isUpLoading = NO;
    self.count = 30;
    [self GetVideoAFN];
}

-(void)getFooterData
{
    self.isUpLoading = YES;
    self.count +=30;
    [self GetVideoAFN];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.groupArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    TBCVideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[TBCVideoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellback1.jpg"]];
        
    }
    TBCVideo *video = [self.groupArray objectAtIndex:indexPath.row];
    cell.video = video;
    tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    TBCVideo *video = [self.groupArray objectAtIndex:indexPath.row];
    MPMoviePlayerViewController *moviePlayerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:video.mp4_url]];
    
    moviePlayerVC.view.frame = CGRectMake(0, 0, self.view.frame.size.width, 300);
    [self.view addSubview:moviePlayerVC.view];
    
    [self presentViewController:moviePlayerVC animated:YES completion:^{
        
        
    }];
    
}

//行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TBCVideo *video = [self.groupArray objectAtIndex:indexPath.row];
    NSString *stringHeight = video.video_height;
    NSString *stringWidth = video.video_width;
    return stringHeight.integerValue * (350*WIDTHSUIT) / stringWidth.integerValue + 150;
}

-(void)GetVideoAFN
{
    
    [TBCAppTools getLocalDataWithURL:[NSString stringWithFormat:@"http://ic.snssdk.com/neihan/stream/mix/v1/?content_type=-104&iid=3116205395&min_time=1444198156&message_cursor=0&count=%ld&latitude=38.88259758462358&mpic=1&essence=1&content_type=-104&ac=WIFI&app_name=joke_essay&aid=7&version_code=4.3.5&device_platform=iphone&os_version=9.0&device_type=iPhone8,1&device_id=6717139371&vid=FAB8E59B-6E8C-4BDA-8D30-E9E7DBB53D16&openudid=7180192250c6112714fd4461d51a8b13cf03302c&os_api=18&idfa=CB9E1D9F-3B84-4BCE-9CC1-20FA8EF975F3&screen_width=750",self.count] TBCToolsBlock:^(id responseObject) {
        
        NSMutableDictionary *dic = responseObject;
        NSMutableDictionary *dataDic = [dic objectForKey:@"data"];
        NSMutableArray *array = [dataDic objectForKey:@"data"];
        
        for (NSMutableDictionary *newDic in array) {
            if (![newDic.allKeys containsObject:@"group"]) {
                continue;
            }
            NSMutableDictionary *groupDic = [newDic objectForKey:@"group"];
            TBCVideo *video = [[TBCVideo alloc] init];
            [video setValuesForKeysWithDictionary:groupDic];
            //video.mp4_url = [groupDic objectForKey:@"mp4_url"];
            video.url = [[[[groupDic objectForKey:@"medium_cover"] objectForKey:@"url_list"] objectAtIndex:1] objectForKey:@"url"];
            video.avatar_url = [[groupDic objectForKey:@"user"] objectForKey:@"avatar_url"];
            video.name = [[groupDic objectForKey:@"user"] objectForKey:@"name"];
            [self.groupArray addObject:video];
            //            NSLog(@"video.mp4_url=%@",video.mp4_url);
            //            NSLog(@"video.url=%@",video.url);
            //            NSLog(@"self.groupArray=%@",self.groupArray);
        }
        [self.progress hide:YES];
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        
    }];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
